<template>
  <div class="app-container">

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['system:sysGroupCategory:add']">新增</el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="sysGroupDataList">
      <el-table-column label="编号" align="center" prop="id" width="60" />
      <el-table-column v-for="(item, index) in tableColumn" :key="index" :label="item.name" :prop="'groupValue.' + item.field " align="center" />
      <el-table-column label="数据字段" align="center" prop="groupKey" />
      <el-table-column label="数据状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.common_data_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:sysGroupCategory:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['system:sysGroupCategory:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改组合数据对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <div v-for="(item, index) in tableColumn" :key="index">
          <el-form-item :label="item.name">
            <el-input v-model="form.groupValue[item.field]" placeholder="请输入数据字段" v-if="item.type == 'input'" />
            <image-upload v-model="form.groupValue[item.field]" v-if="item.type == 'imageUpload'" :limit="1" />
          </el-form-item>
        </div>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序" type="number" />
        </el-form-item>
        <el-form-item label="数据状态">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in dict.type.common_data_status" :key="dict.value" :label="parseInt(dict.value)">{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { getSysGroupCategory } from "@/api/system/sysGroupCategory";
import {
  listSysGroupData,
  getSysGroupData,
  delSysGroupData,
  addSysGroupData,
  updateSysGroupData,
} from "@/api/system/sysGroupData";
export default {
  dicts: ["common_data_status"],
  data() {
    return {
      categoryId: 0,
      tableData: [],
      tableColumn: [],
      categoryInfo: {},
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 组合数据表格数据
      sysGroupDataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        categoryId: null,
        groupValue: null,
        groupKey: null,
        sort: null,
        status: null,
      },
      // 表单参数
      form: {
        groupValue: {},
      },
      // 表单校验
      rules: {},
    };
  },
  created() {
    const categoryId = this.$route.params && this.$route.params.categoryId;
    this.categoryId = categoryId;
    this.getSysGroupCategoryInfo();
    this.getList();
  },
  methods: {
    getSysGroupCategoryInfo() {
      getSysGroupCategory(this.categoryId).then((res) => {
        this.categoryInfo = res.data;
        this.tableColumn = JSON.parse(res.data.content);
      });
    },

    // 表单重置
    reset() {
      let tableColumn = this.tableColumn;
      let groupValue = {};
      tableColumn.forEach((item) => {
        groupValue[item.field] = null;
      });
      this.form = {
        id: null,
        categoryId: null,
        groupValue: groupValue,
        groupKey: null,
        sort: null,
        delFlag: null,
        status: 0,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };

      this.resetForm("form");
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加组合数据";
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.groupValue = JSON.stringify(this.form.groupValue);
          this.form.categoryId = this.categoryId;
          console.log(this.categoryInfo);
          this.form.groupKey = this.categoryInfo.categoryKey;
          if (this.form.id != null) {
            updateSysGroupData(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSysGroupData(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    getList() {
      this.loading = true;
      listSysGroupData({
        categoryId: this.categoryId,
      }).then((response) => {
        let sysGroupDataList = response.rows;
        sysGroupDataList.forEach((item) => {
          item.groupValue = JSON.parse(item.groupValue);
        });
        this.sysGroupDataList = sysGroupDataList;
        console.log(sysGroupDataList);
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getSysGroupData(id).then((response) => {
        this.form = response.data;
        this.form.groupValue = JSON.parse(this.form.groupValue);
        this.open = true;
        this.title = "修改组合数据";
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除组合数据编号为"' + ids + '"的数据项？')
        .then(function () {
          return delSysGroupData(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
  },
};
</script>

<style>
</style>